#include "stdafx.h"
#include "CListCtrlEx.h"

IMPLEMENT_DYNAMIC(CListCtrlEx, CListCtrl)

BEGIN_MESSAGE_MAP(CListCtrlEx, CListCtrl)
	ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, &CListCtrlEx::OnNMCustomdraw)
END_MESSAGE_MAP()

//CustomDraw to change selected item color
void CListCtrlEx::OnNMCustomdraw(NMHDR* pNMHDR, LRESULT* pResult)
{
	NMLVCUSTOMDRAW* pLVCD = reinterpret_cast<NMLVCUSTOMDRAW*>(pNMHDR);
	
	*pResult = CDRF_DODEFAULT;
	
	switch (pLVCD->nmcd.dwDrawStage)
	{
		case CDDS_PREPAINT:
			*pResult = CDRF_NOTIFYITEMDRAW;
			break;
		case CDDS_ITEMPREPAINT:
			*pResult = CDRF_NOTIFYSUBITEMDRAW;
			break;
		case CDDS_SUBITEM | CDDS_ITEMPREPAINT:
		{
			if ((GetItemState(pLVCD->nmcd.dwItemSpec, LVIS_SELECTED) & LVIS_SELECTED))
			{
				pLVCD->nmcd.uItemState = CDIS_DEFAULT;

				pLVCD->clrTextBk = RGB(135, 206, 118);
				pLVCD->clrText = GetSysColor(COLOR_HIGHLIGHTTEXT);
				*pResult = CDRF_NEWFONT;
			}
			break;
		}

		default:
			break;
	}
}

void CListCtrlEx::PreSubclassWindow()
{
	CHeaderCtrl* pHeader = GetHeaderCtrl();
	if (pHeader != NULL) { VERIFY(m_HeaderCtrl.SubclassWindow(pHeader->m_hWnd)); }

	CListCtrl::PreSubclassWindow();
}

void CListCtrlEx::RegisterHeaderCtrl()
{
	CHeaderCtrl* pHeader = GetHeaderCtrl();
	if (pHeader != NULL) { VERIFY(m_HeaderCtrl.SubclassWindow(pHeader->m_hWnd)); }
}
